
{% extends 'base.html' %}

{% block css%}
<style type="text/css">

</style>
{% endblock %}

{% block body%} 
<div class="row">

	<div class="col-md-8">
			<button id="btn_get_all" class="btn btn-success pull-right" style="margin-bottom:10px">
			Get all
			&nbsp;&nbsp;
			<i id="btn_get_all_loading" class="fa fa-refresh fa-spin fa-fw"></i>
			</button>
			<table id="t_analog" class="table table-hover table-condensed">
				<tr>
					<th width="40%">Name</th>
					<th>Unit</th>
					<th>Aval</th>
					<th>Rawval</th>
					<th>Dval</th>
					<th><span class="pull-right">Operate</span></th>
				</tr>
				
				{% for adc_item in adc_items %}
					<tr>

						<td style="font-size:12px">
							{{ adc_item[0] }}
						</td>

						<td>
							{{ adc_item[1] }}
						</td>


						<td id="adc_val2_{{ adc_item[0] }}">
						</td>

						<td id="adc_val3_{{ adc_item[0] }}">
						</td>

						<td id="adc_val1_{{ adc_item[0] }}">
						</td>

						<td>
							<button class="btn btn-success pull-right" onClick="btn_adc_evt('{{ adc_item[0] }}')">
							Get
							</button>
						</td>
					</tr>
				{% endfor %}

			</table>
	</div>

</div>
{% endblock %}

{% block js%} 
{{ super() }}
<script type="text/javascript">

	function btn_adc_evt(item)
	{
		$.post("api/get_adc",
        {
            item:item,
        },
        function(data,status){
            console.log("data: " + data);
			cells = data.split(",");
			$("#adc_val1_" + item).html(cells[0]);
			$("#adc_val2_" + item).html(cells[1]);
			$("#adc_val3_" + item).html(cells[2]);
        });
	}


	function btn_get_all()
	{
		$("#btn_get_all_loading").show();
		$.post("api/get_adc_all",
        {
        },
        function(data,status){
            console.log(data);
            d = JSON.parse(data);
            $.each(d,function(index,value){
                // $("#analog1_val_" + index).html(value);	

                cells = value.split(',');
				$("#adc_val1_" + index).html(cells[0]);
				$("#adc_val2_" + index).html(cells[1]);
				$("#adc_val3_" + index).html(cells[2]);

				// save to session
				sessionStorage.setItem("adc_val1_" + index, cells[0]);
				sessionStorage.setItem("adc_val2_" + index, cells[1]);
				sessionStorage.setItem("adc_val3_" + index, cells[2]);

				$("#btn_get_all_loading").hide();
            });
        });
	}


	function read_session_data()
	{
		adc_names = [
						'TX_DRIVER_PI_XI', 'TX_DRIVER_PI_XQ',
						'TX_DRIVER_PI_YI', 'TX_DRIVER_PI_YQ',
						'COSA_TEMP_OUT',  'TX_DRV_VOCM',
						'RX_TIA_PI_XI', 'VOFE_CURRENT_SEN', 
						'MCU1_RX_TIA_PI_YI', 'TX_VOA_X', 'TX_VOA_Y',
						'REF2V5_MCU_ABC', 'TX_VOA_X_I', 'TX_VOA_Y_I',
						'MCU1INTERNAL_TEMP_SNS', 'MCU1INTERNAL_AVDD',
						'MCU1INTERNAL_IOVDD0', 'MCU1INTERNAL_IOVDD1',
						'P0V55_DSP_VDDC', 'P0V75_DSP_VDDM',
						'P0V94_DSP_VDDA', 'P1V8_DSP_VDDA18',
						'P1V2_DSP_VDDA12', 'P3V_ABC_TXMPD',
						'PCB_TEMP_ADC',  'DSP_TEMP', 
						'POST_MPDX_DC_MCU2ADC', 'POST_MPDY_DC_MCU2ADC',
						'POST_MPDXY_AC_MCU2ADC', 'RX_VOA_X',
						'TIA_PD_BIAS', 'TIA_VCC', 'RX_VOA_Y',
						'RX_MPD_X_MCU2', 'RX_TIA_PI_XQ',
						'RX_TIA_PI_YI', 'RX_TIA_PI_YQ', 'RX_VOA_X_I',
						'RX_MPD_Y_MCU2', 'RX_VOA_Y_I',
						'MCU2INTERNAL_TEMP_SNS', 'MCU2INTERNAL_AVDD',
						'MCU2INTERNAL_IOVDD0', 'MCU2INTERNAL_IOVDD1',
						'ADC_TIA_VGC_YQ', 'ADC_TIA_VGC_YI',
						'ADC_TIA_VGC_XQ', 'ADC_TIA_VGC_XI',
						'TX_DRV_VCC', 'P5V8_DRIVER_VOFE', 'P6V_VOA',
						'COSA_PH_BIAS'
             ];

		for(idx = 0; idx < adc_names.length; idx++)
		{
			$("#adc_val1_"+adc_names[idx]).html(sessionStorage.getItem("adc_val1_"+adc_names[idx]));
			$("#adc_val2_"+adc_names[idx]).html(sessionStorage.getItem("adc_val2_"+adc_names[idx]));
			$("#adc_val3_"+adc_names[idx]).html(sessionStorage.getItem("adc_val3_"+adc_names[idx]));
		}
	}


	$(document).ready(function(){
		$("#btn_get_all_loading").hide();
		read_session_data();

		//var tr_num = $('#t_analog tr').size();
		// $('#btn_get_all').click(function(){
		// 	var item = ''
		// 	$('#t_analog tr').each(function(i, val){
		// 		$(this).children('td').each(function(j){ 
		// 			//alert("第"+(i)+"行，第"+(j)+"个td的值："+$(this).text());
		// 			if(j == 0){
		// 				item = $.trim($(this).text());
		// 				//alert(i + ": " + item)
		// 				// alert(item);
		// 				btn_adc_evt(item);
		// 				return false;
		// 			}
		// 		});
		// 	});
		// });


		// get all --new method
		$('#btn_get_all').click(function(){
			btn_get_all();
		});


	});
</script>
{% endblock %}